﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20.ZICModel;
using Newtonsoft.Json;
using CCF20Model;
using System.ServiceModel;
using System.Windows;
using System.Diagnostics;


namespace CCF20
{
    
    class CCFZIC
    {
        private string PID = "1";
        private string Pwd = "123";
        
        private VehicleModel vehicleModel;
        private FaultModel faultModel;
        private DeviceModel deviceModel;
        private VehicleMaintenanceModel maintenanceModel;
        public CCFZIC()
        {
            vehicleModel = new VehicleModel();
            faultModel = new FaultModel();
            deviceModel = new DeviceModel();
            PID = ZICMonitorModel.ZICLoginInfo.IDSName;
            Pwd = ZICMonitorModel.ZICLoginInfo.IDSPassword;
            if (ZICMonitorModel.IsJava)
            {
                LoggerHelper.LogCCF20(string.Format("ZIC Url: {0}", ZICMonitorModel.ZICJava.Url));
            }
            else
            {
                LoggerHelper.LogCCF20(string.Format("ZIC Url: {0}", ZICMonitorModel.ZICSvc.Url));
            }
            
        }

       

        public void GetFHList()
        {
            int anchiID = deviceModel.GetAnchiID();
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.GetFhList(PID, Pwd, anchiID);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.GetFhList(PID, Pwd, anchiID);
            }
            LoggerHelper.LogCCF20(string.Format("GetFhList IDS={0},AnchiID={1}",PID,anchiID));
            if (ret == null || ret == string.Empty)
            {
                return;
            }
            else
            {
                if (ret.Equals("LoginErr", StringComparison.InvariantCultureIgnoreCase))
                {
                    MessageBox.Show("IDS 登入名和密码不匹配", "CCF2.0");
                    return;
                }
            }
            List<FhList> list = (List<FhList>)JavaScriptConvert.DeserializeObject(ret, typeof(List<FhList>));
            
            foreach (FhList model in list)
            {
                Device device = new Device();
                device.AnchiID = Convert.ToInt32(model.Xsid); //id in the sales list is used for AnchiID
                if (!model.Idc.StartsWith("86"))
                {
                    device.IDCSimNumber = "86" + model.Idc;
                }
                else
                {
                    device.IDCSimNumber = model.Idc;
                }
                device.DeviceKey = model.Xlh;
                device.Invoice = model.Ss_paper;
                device.DeviceType = model.Cpxh;
                device.DeviceStatus = "在途";
                device.IsManual = false;
                deviceModel.AddDevice(device);

            }


        }
        public bool UploadCustomerInfo(Device device)
        {
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.UpdMesByUpdClient(
                                            PID,
                                            Pwd,
                                            device.IDCSimNumber,
                                            device.Vehicle.VehicleType.Model,
                                            device.Vehicle.VehicleLicense,
                                            device.Vehicle.Customer.Name,
                                            device.Vehicle.EngineNo,
                                            device.Vehicle.ChassiNo,
                                            device.Vehicle.VehicleType.Displacement,
                                            device.Vehicle.VehicleType.EngineType);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.UpdMesByUpdClient(
                                               PID,
                                               Pwd,
                                               device.IDCSimNumber,
                                               device.Vehicle.VehicleType.Model,
                                               device.Vehicle.VehicleLicense,
                                               device.Vehicle.Customer.Name,
                                               device.Vehicle.EngineNo,
                                               device.Vehicle.ChassiNo,
                                               device.Vehicle.VehicleType.Displacement,
                                               device.Vehicle.VehicleType.EngineType);
            }
            return ValideReturnMessageFromZIC(ret);                        
        }
        public bool DeviceRegister(Device device, string is5101CallCenter)
        {
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.ProductRegister(
                                             device.DeviceKey,
                                             device.IDCSimNumber,
                                             PID,
                                             Pwd,
                                             device.Vehicle.VehicleLicense,
                                             device.Customer.Name,
                                             "32",
                                             device.Vehicle.VehicleType.Model,
                                             device.Customer.IdentityNo,
                                             device.DeviceType,
                                             "RDS",
                                             device.Vehicle.EngineNo,
                                             device.Vehicle.ChassiNo,
                                             is5101CallCenter,
                                             device.Vehicle.VehicleType.Displacement,
                                             device.Vehicle.VehicleType.EngineType);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.ProductRegister(device.DeviceKey,
                                             device.IDCSimNumber,
                                             PID,
                                             Pwd,
                                             device.Vehicle.VehicleLicense,
                                             device.Customer.Name,
                                             "32",
                                             device.Vehicle.VehicleType.Model,
                                             device.Customer.IdentityNo,
                                             device.DeviceType,
                                             "CCF",
                                             device.Vehicle.EngineNo,
                                             device.Vehicle.ChassiNo,
                                             is5101CallCenter,
                                             device.Vehicle.VehicleType.Displacement,
                                             device.Vehicle.VehicleType.EngineType);
            }
            
            return ValideReturnMessageFromZIC(ret);
            

        }
        public bool DeviceUnRegister(Device device)
        {
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.cancelproductRegister(device.IDCSimNumber,
                                                    PID,
                                                    Pwd,
                                                    device.DeviceKey);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.cancelproductRegister(device.IDCSimNumber,
                                                    PID,
                                                    Pwd,
                                                    device.DeviceKey);
            }
            return ValideReturnMessageFromZIC(ret);
            
        }
        public bool SpecialIDCRegister(Device device)
        {
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.SpecialIDcReg(PID, Pwd, device.IDCSimNumber);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.SpecialIDcReg(PID, Pwd, device.IDCSimNumber);
            }

            return ValideReturnMessageFromZIC(ret);
        }
        public bool SpecialIDCUnRegister(Device device)
        {
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.cancelIDcReg(PID, Pwd, device.IDCSimNumber);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.cancelIDcReg(PID, Pwd, device.IDCSimNumber);
            }
            
            return ValideReturnMessageFromZIC(ret);
        }
        public bool IsSpecialIDC(Device device)
        {
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.IsSpIDC(PID, Pwd, device.IDCSimNumber);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.IsSpIDC(PID, Pwd, device.IDCSimNumber);
            }
            return ValideReturnMessageFromZIC(ret);
            
        }
        public bool ValideReturnMessageFromZIC(string ret)
        {
            if (ret == null)
            {
                StackTrace stackTrace = new StackTrace();
                LoggerHelper.LogCCF20(string.Format("ZIC 调用:{0} 返回为空，请与智信通联系", stackTrace.GetFrame(1).GetMethod().Name));
                return false;
            }
            switch (ret.ToLower())
            {
                case "exist":
                    return true;
                case "true":
                    return true;
                case "false":
                    return false;
                case "client_error":
                    MessageBox.Show("已注册过", "ZIC返回消息");
                    return true;
                case "shopid_error":
                    MessageBox.Show("公司不匹配", "ZIC返回消息");
                    return false;
                case "not_sell":
                case "nosell":
                    MessageBox.Show("此IDC没有销售过", "ZIC返回消息");
                    return false;
                case "err":
                    MessageBox.Show("数据有错", "ZIC返回消息");
                    return false;
                case "idssame":
                    MessageBox.Show("同一个IDS", "ZIC返回消息");
                    return false;
                case "no_fit":
                case "nofit":
                    MessageBox.Show("不存在这个IDC", "ZIC返回消息");
                    return false;
                case "replace_true":
                    MessageBox.Show("替换成功", "ZIC返回消息");
                    return true;
                case "replace_false":
                    MessageBox.Show("替换失败", "ZIC返回消息");
                    return false;
                
                default:
                    MessageBox.Show(ret, "ZIC返回消息");
                    return false;
            }
            
            
        }
        public void GetVehicleType()
        {
            maintenanceModel = new VehicleMaintenanceModel();
            int temp = maintenanceModel.GetMaxVehicleTypeID();
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.GetCarTy(PID, Pwd, temp.ToString());
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.GetCarTy(PID, Pwd, temp.ToString());
            }
            if (ret == null) return;
            List<CarTyModel> list = (List<CarTyModel>)JavaScriptConvert.DeserializeObject(ret, typeof(List<CarTyModel>));

            foreach (CarTyModel model in list)
            {
                //if ((ZICMonitorModel.IsJava && model.brand.Trim() != "FAW-VW") ||
                //    (!ZICMonitorModel.IsJava && model.brand.Trim() == "FAW-VW"))
                //{
                    VehicleType vehicleType = new VehicleType();
                    vehicleType.AnchiID = model.id;
                    vehicleType.Model = model.carType.Trim();
                    vehicleType.Manufacture = model.brand.Trim();
                    vehicleType.EngineType = model.Engine_type.Trim();
                    vehicleType.Displacement = model.displacement.Trim();
                    maintenanceModel.UpdateVehicleType(vehicleType);
                //}  
            }
            maintenanceModel.Update();
        }
        public void GetMaintenanceType()
        {
            maintenanceModel = new VehicleMaintenanceModel();
            int temp = maintenanceModel.GetMaxMaintenanceTypeID();
            string ret;
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.GetTymaintain(PID, Pwd, temp.ToString());
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.GetTymaintain(PID, Pwd, temp.ToString());
            }
            if (ret == null) return;
            List<TymaintainModel> list = (List<TymaintainModel>)JavaScriptConvert.DeserializeObject(ret, typeof(List<TymaintainModel>));

            foreach (TymaintainModel model in list)
            {
                
                LoggerHelper.LogCCF20(string.Format("TyID:{0}\tAnchiID:{1}\tName:{2}",model.tyid,model.id,model.MaintainPj.Trim()));
                MaintenanceType maintenanceType = new MaintenanceType();
                maintenanceType.VehicleTypeID = (model.tyid == null ? 0 : Convert.ToInt32(model.tyid));
                maintenanceType.TyID = model.tyid;
                maintenanceType.AnchiID = model.id;
                maintenanceType.ServicePeriod = (model.MaintainPeriod == null ? 0 : Convert.ToInt32(model.MaintainPeriod));
                maintenanceType.Name = model.MaintainPj.Trim();
                maintenanceType.ServiceMileage = (model.mileage == null ? 0 : Convert.ToInt32(model.mileage));
                maintenanceType.Priority = 1;
                maintenanceModel.UpdateMaintenanceType(maintenanceType);
                maintenanceModel.Update();
            }
            
        }
        public string ClearCode(string idc, int codeType)
        {
            if (ZICMonitorModel.IsJava)
            {
                return ZICMonitorModel.ZICJava.ClearCodes(PID, Pwd, idc, 14, codeType);
            }
            else
            {
                return ZICMonitorModel.ZICSvc.ClearCodes(PID, Pwd, idc, 14, codeType);
            }
            
        }
        public bool TransferCustomer(TransferedCustomer transfer)
        {
            string ret = string.Empty;
            if (ZICMonitorModel.IsJava)
            {
                ret =ZICMonitorModel.ZICJava.EditDevice(PID,Pwd,transfer.DeviceKey,transfer.IDCNum);
                                                   

            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.EditDevice(PID, Pwd, transfer.DeviceKey, transfer.IDCNum);
            }
            return ValideReturnMessageFromZIC(ret);

        }
        public void ConfirmTransferCustomer()
        {
            string ret = string.Empty;
            DateTime? lastTransfer = deviceModel.GetConfirmTransferDateTime();
            if (lastTransfer == null)
            {
                lastTransfer = DateTime.Now- new TimeSpan(0,1,0,0);
            }
            if (ZICMonitorModel.IsJava)
            {
                ret = ZICMonitorModel.ZICJava.GetZYList(PID, Pwd, (DateTime)lastTransfer);
            }
            else
            {
                ret = ZICMonitorModel.ZICSvc.GetZYList(PID, Pwd, (DateTime)lastTransfer);
            }
            
            if (ret == null || ret == string.Empty)
            {
                return;
            }
            List<ConFirmDeviceModel> list = (List<ConFirmDeviceModel>)JavaScriptConvert.DeserializeObject(ret, typeof(List<ConFirmDeviceModel>));
            foreach (ConFirmDeviceModel device in list)
            {
                string deviceKey = deviceModel.GetDeviceKeyBySimNumber(device.DeviceId);
                int customerID = deviceModel.GetCustomerIDByTransferSimNumber(device.DeviceId);
                int vehicleID = deviceModel.GetVehicleIDByTransferSimNumber(device.DeviceId);
                if ( deviceKey == string.Empty || customerID ==0 ) continue;

                Device confirmedDevice = new Device();
                confirmedDevice.CustomerID = customerID;
                DateTime registerDate;
                if (DateTime.TryParse(device.OutDate, out registerDate))
                {
                    confirmedDevice.RegisterDate = registerDate;
                }else
                {
                    LoggerHelper.LogCCF20(string.Format("确认转移设备 不正确的注册日期={0}",device.OutDate));
                }
                confirmedDevice.DeviceType = device.deviceType;
                confirmedDevice.IDCSimNumber = device.DeviceId;
                confirmedDevice.TerminalType = device.terminalType;
                confirmedDevice.IsRegistered = true;
                confirmedDevice.IsActive = true;
                confirmedDevice.DeviceKey = deviceKey;
                confirmedDevice.IsTransfer = true;
                confirmedDevice.DeviceStatus = "售出";
                confirmedDevice.CustomerID = customerID;
                confirmedDevice.VehicleID = vehicleID;
                deviceModel.AddDevice(confirmedDevice);
                MessageBox.Show(string.Format("IDC={0}转移成功", confirmedDevice.IDCSimNumber), "CCF2.0");
                deviceModel.UpdateConfirmTransferDateTime();
            }
            
            LoggerHelper.LogCCF20(string.Format("GetZYList IDS={0},Count={1}", PID, list.Count));
            

        }
        public string UpdateCompanyProfile(string LoginName,
                                            string CompanyName,
                                            string ExcuteMan,
                                            string LinkPhone,
                                            string CompanyPhone,
                                            string CompanyFax,
                                            string CompanyWebSite,
                                            string CompanyAddress)
        {
            string ret = string.Empty;
            if (ZICMonitorModel.IsJava)
            {
                ZICMonitorModel.ZICJava.UpdateCustomInfo(PID,
                                              Pwd,
                                              LoginName,
                                              CompanyName,
                                              ExcuteMan,
                                              LinkPhone,
                                              CompanyPhone,
                                              CompanyFax,
                                              CompanyWebSite,
                                              CompanyAddress);
            }
            else
            {
                ZICMonitorModel.ZICSvc.UpdateCustomInfo(PID,
                                              Pwd,
                                              LoginName,
                                              CompanyName,
                                              ExcuteMan,
                                              LinkPhone,
                                              CompanyPhone,
                                              CompanyFax,
                                              CompanyWebSite,
                                              CompanyAddress);
            }
            
            if (ret == null) return "";
            switch (ret.ToLower())
            {
                case "true": 
                    ret = "更新成功";
                    break;
                case "false":
                    ret = "更新失败";
                    break;
                case "loginerr":
                    ret = "登录失败";
                    break;
                case "err":
                    ret = "数据有误";
                    break;
            }
            return ret;
        }
        public void SendGPRSMessage(string txt)
        {
            if (ZICMonitorModel.IsJava)
            {
                ZICMonitorModel.ZICJava.AddGPRSMessage(PID,
                                              Pwd,
                                              txt);
                                              
            }
            else
            {
                ZICMonitorModel.ZICSvc.AddGPRSMessage(PID,
                                              Pwd,
                                              txt);
            }
            
        }

    }
}
